/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package todonet.serializers;

import java.io.InputStream;
import java.io.OutputStream;
import todonet.ToDoTask;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XppDriver;
import java.io.FileWriter;
import java.io.StringWriter;

/**
 *
 * @author DNS
 */
//@SupportedFileExtension(".todo.xml")
//@SupportedFileExtension(".todo")
public class ToDoXmlSerializer implements IToDoSerializer {

//    public void Save(OutputStream outStream, ToDoTask rootTask) {
//       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//       DocumentBuilder builder = null;
//        try {
//            builder = factory.newDocumentBuilder();
//        } catch (ParserConfigurationException ex) {
//            Logger.getLogger(ToDoXmlSerializer.class.getName()).log(Level.SEVERE, null, ex);
//        }
//       Document doc = builder.newDocument();
//       Element RootElement = doc.createElement(String.valueOf(rootTask));

//       Element ChildElement = doc.createElement("important");
//       ChildElement.appendChild(doc.createTextNode(String.valueOf(taskList.get(2))));
//       RootElement.appendChild(ChildElement);
//
//       Element ChildElement1 = doc.createElement("percentComplete");
//       ChildElement1.appendChild(doc.createTextNode(String.valueOf(taskList.get(3))));
//       RootElement.appendChild(ChildElement1);
//
//       Element ChildElement2 = doc.createElement("dateofEnd");
//       ChildElement2.appendChild(doc.createTextNode(String.valueOf(taskList.get(4))));
//       RootElement.appendChild(ChildElement2);
//
//       doc.appendChild(RootElement);
//
//       Transformer t = null;
//        try {
//            t = TransformerFactory.newInstance().newTransformer();
//        } catch (TransformerConfigurationException ex) {
//            Logger.getLogger(ToDoXmlSerializer.class.getName()).log(Level.SEVERE, null, ex);
//        }
//      // t.setOutputProperty(OutputKeys.METHOD, "xml");
//       t.setOutputProperty(OutputKeys.INDENT, "yes");
//        try {
//            t.transform(new DOMSource(doc), new StreamResult(outStream));
//        } catch (TransformerException ex) {
//            Logger.getLogger(ToDoXmlSerializer.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }

    public void Save (OutputStream outStream, ToDoTask rootTask)
    {
        XStream serializer = new XStream(new DomDriver("UTF-8"));
        serializer.alias("ToDoTask", ToDoTask.class);
        serializer.aliasField("childs", ToDoTask.class, "_childs");
        serializer.aliasField("parent", ToDoTask.class, "_parent");
        serializer.aliasField("title", ToDoTask.class, "_title");
        serializer.aliasField("important", ToDoTask.class, "_important");
        serializer.aliasField("percentComplete", ToDoTask.class, "_percentComplete");
        serializer.aliasField("dateOfEnd", ToDoTask.class, "_dateOfEnd");
        serializer.toXML(rootTask, outStream);
    }
    
    public ToDoTask Load(InputStream inStream) {
        XStream serializer = new XStream(new DomDriver("UTF-8"));
        serializer.alias("ToDoTask", ToDoTask.class);
        serializer.aliasField("childs", ToDoTask.class, "_childs");
        serializer.aliasField("parent", ToDoTask.class, "_parent");
        serializer.aliasField("title", ToDoTask.class, "_title");
        serializer.aliasField("important", ToDoTask.class, "_important");
        serializer.aliasField("percentComplete", ToDoTask.class, "_percentComplete");
        serializer.aliasField("dateOfEnd", ToDoTask.class, "_dateOfEnd");
        return (ToDoTask) serializer.fromXML(inStream);
    }

//    public List<ToDoTask> Load(InputStream inStream) {
//        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//        factory.setValidating(false);
//        DocumentBuilder builder = null;
//        try {
//            builder = factory.newDocumentBuilder();
//        } catch (ParserConfigurationException ex) {
//            Logger.getLogger(ToDoXmlSerializer.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        Document doc = null;
//        try {
//            doc = builder.parse(inStream);
//        } catch (SAXException ex) {
//            Logger.getLogger(ToDoXmlSerializer.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IOException ex) {
//            Logger.getLogger(ToDoXmlSerializer.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        Node node = doc.getChildNodes().item(0);
//
//        return null;
//    }

}
